﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Somnus.Core.SceneManager;
using Microsoft.Xna.Framework.Input;

namespace Somnus.Core.Pause
{
    class PauseScreen
    {
        private GaussianBlur gaussianBlur;
        private Texture2D pauseTexture;
        private RenderTarget2D renderTarget1;
        private RenderTarget2D renderTarget2;
        private int renderTargetWidth;
        private int renderTargetHeight;
        private Color pauseColor;
        private Texture2D pauseBackGround;
        public float alpha;

        private Texture2D TEXTO;

        // WARNING:
        // If you change the BLUR_RADIUS you *MUST* also change the RADIUS
        // constant in GaussianBlur.fx. Both values *MUST* be the same.

        private const int BLUR_RADIUS = 2;
        public float blurAmount = 30;

        enum PauseStatus
        {
            PAUSING,
            PAUSED,
            UNPAUSING
        }

        PauseStatus pauseStatus;


        public PauseScreen(GraphicsDevice graphicsDevice)
        {
            gaussianBlur = new GaussianBlur(Game1.getInstance());
            gaussianBlur.ComputeKernel(BLUR_RADIUS, blurAmount);

            InitRenderTargets(graphicsDevice);

            pauseColor = Color.White;
            pauseBackGround = Game1.getInstance().Content.Load<Texture2D>("Pause_Paralaxis");
            TEXTO = Game1.getInstance().Content.Load<Texture2D>("Pause_Text");

            PauseParticle.Initialize(graphicsDevice);
        }

        public void preparePause(GameTime gameTime, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice)
        {
            pauseColor = Color.White;
            blurAmount = 9;
            alpha = 0;
            pauseStatus = PauseStatus.PAUSING;

            //Salva o tamanho da tela
            int w = graphicsDevice.PresentationParameters.BackBufferWidth;
            int h = graphicsDevice.PresentationParameters.BackBufferHeight; 

            //Cria e seta um novo target para o Render
            RenderTarget2D pauseRenderTarget = new RenderTarget2D(graphicsDevice, w, h);
            graphicsDevice.SetRenderTarget(pauseRenderTarget);

            //Desenha o jogo nesse novo render target
            SceneManager.SceneManager.justDraw(gameTime, spriteBatch);
            
            //Salva o render target em uma textura
            pauseTexture = pauseRenderTarget as Texture2D;
            
            //Seta o renderTarget para o BackBuffer
            graphicsDevice.SetRenderTarget(null);

            //Limpa o device
            graphicsDevice.Clear(Color.CornflowerBlue);
        }

        private void InitRenderTargets(GraphicsDevice graphicsDevice)
        {
            renderTargetWidth = graphicsDevice.PresentationParameters.BackBufferWidth / 2;
            renderTargetHeight = graphicsDevice.PresentationParameters.BackBufferHeight / 2;

            renderTarget1 = new RenderTarget2D(graphicsDevice,
                renderTargetWidth, renderTargetHeight, false,
                graphicsDevice.PresentationParameters.BackBufferFormat,
                DepthFormat.None);

            renderTarget2 = new RenderTarget2D(graphicsDevice,
                renderTargetWidth, renderTargetHeight, false,
                graphicsDevice.PresentationParameters.BackBufferFormat,
                DepthFormat.None);

            gaussianBlur.ComputeOffsets(renderTargetWidth, renderTargetHeight);
        }

        public void update(GameTime gameTime)
        {
            switch (pauseStatus)
            {
                case PauseStatus.PAUSING:
                    //Ajusta o blur
                    blurAmount = MathHelper.Lerp(blurAmount, 1f, 0.05f);
                    alpha = MathHelper.Lerp(alpha, 1f, 0.05f);
                    gaussianBlur.ComputeKernel(BLUR_RADIUS, blurAmount);

                    //Ajusta a cor
                    pauseColor = Color.Lerp(pauseColor, Color.Lerp(Color.Lerp(Color.White, Color.RoyalBlue, 0.15f), Color.Purple, 0.15f), 0.1f);

                    if (alpha > 0.8f)
                    {
                        
                        pauseStatus = PauseStatus.PAUSED;
                    }
                            
                    break;

                case PauseStatus.PAUSED:

                    //Ajusta o blur
                    blurAmount = MathHelper.Lerp(blurAmount, 1f, 0.05f);
                    alpha = MathHelper.Lerp(alpha, 1f, 0.05f);
                    gaussianBlur.ComputeKernel(BLUR_RADIUS, blurAmount);

                    //Ajusta a cor
                    //pauseColor = Color.Lerp(pauseColor, new Color(218, 206, 242), 0.1f);
                    pauseColor = Color.Lerp(pauseColor, Color.Lerp(Color.Lerp(Color.White, Color.RoyalBlue, 0.15f), Color.Purple, 0.15f), 0.1f);

                    if (Controller.KeyPressed(Keys.Escape))
                        pauseStatus = PauseStatus.UNPAUSING;
                    
                    break;


                case PauseStatus.UNPAUSING:
                    
                    blurAmount = MathHelper.Lerp(blurAmount, 9f, 0.1f);
                    alpha = MathHelper.Lerp(alpha, 0f, 0.1f);
                    gaussianBlur.ComputeKernel(BLUR_RADIUS, blurAmount);

                    //Ajusta a cor
                    pauseColor = Color.Lerp(pauseColor, Color.White, 0.1f);

                    if (alpha < 0.1f)
                    {
                        SceneManager.SceneManager.isPaused = false;
                    }
                    break;
            }
            
           

            //Update para as particulas
            PauseParticle.Update(gameTime);
        }

        public void draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            Texture2D result = gaussianBlur.PerformGaussianBlur(pauseTexture, renderTarget1, renderTarget2, spriteBatch);
            Texture2D texturaParticulas = PauseParticle.drawParticles(gameTime, spriteBatch, Game1.getInstance().GraphicsDevice);
            Rectangle rectangle = new Rectangle(0, 0, pauseTexture.Width, pauseTexture.Height);

            spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
            spriteBatch.Draw(result, rectangle, pauseColor);
            spriteBatch.Draw(pauseBackGround, rectangle, (Color.White * 0.9f) * alpha);
            spriteBatch.Draw(texturaParticulas, rectangle, Color.White * alpha);
            spriteBatch.Draw(TEXTO, new Rectangle(50, 50, 600, 330), Color.White * alpha);
            spriteBatch.End();
            
        }
    }
}
